package com.xinqi.common.third.word.utils;

import com.google.common.collect.ImmutableList;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.StringReader;
import java.util.List;

/**
 * 分词工具类
 *
 * @author linkai
 * @date 2023/2/22
 */
public final class ParticipleUtils {

    /**
     * 文本分词（使用IK）
     *
     * @param text 文本
     * @return 词列表
     */
    public static List<String> participle(String text) {
        if (null == text || "".equals(text.trim())) {
            return null;
        }
        ImmutableList.Builder<String> words = ImmutableList.builder();
        StringReader re = new StringReader(text);
        IKSegmenter ik = new IKSegmenter(re, true);
        Lexeme lex;
        try {
            while ((lex = ik.next()) != null) {
                words.add(lex.getLexemeText());
            }
        } catch (Exception ignored) {
        }
        return words.build();
    }
}
